package com.github.niefy.modules.wx.handler;

import com.github.niefy.modules.wx.entity.TemplateMsgFinishLog;
import com.github.niefy.modules.wx.service.TemplateMsgFinishLogService;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.ZoneOffset;
import java.util.Map;

/**
 * @author : [86188]
 * @description : 模板消息事件处理
 * @createTime : [2023/1/30 9:18]
 * @updateUser : [86188]
 * @updateTime : [2023/1/30 9:18]
 */
@Component
public class TemplateMsgHandler extends AbstractHandler {

    @Autowired
    TemplateMsgFinishLogService templateMsgFinishLogService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map,
                                    WxMpService wxMpService, WxSessionManager wxSessionManager) {
        var templateMsgFinishLog = TemplateMsgFinishLog.builder()
                .appid(wxMpXmlMessage.getToUser())
                .fromUser(wxMpXmlMessage.getFromUser())
                .msgId(wxMpXmlMessage.getMsgId())
                .statusResult(wxMpXmlMessage.getStatus())
                .createTime(Instant.ofEpochMilli(wxMpXmlMessage.getCreateTime()).atZone(ZoneOffset.ofHours(8)).toLocalDateTime())
                .build();
        templateMsgFinishLogService.save(templateMsgFinishLog);
        return null;
    }
}
